import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
from torch.nn import functional as F


class LeNet5(nn.Module):
    def __init__(self):
        super(LeNet5, self).__init__()
        # loss function
        self.cretenon = F.mse_loss
        # model
        self.CNN_unit = nn.Sequential(
            nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d((2, 2)),
            nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d((2, 2)),
        )
        self.DNN_unit = nn.Sequential(
            nn.Linear(400, 256),
            nn.ReLU(),
            nn.Linear(256, 64),
            nn.ReLU(),
            nn.Linear(64, 10),
        )

    def forward(self, x):
        # [b,1,28,28]
        x = self.CNN_unit(x)
        # [b,12,28,28]
        x = x.view(x.size(0), -1)
        x = self.DNN_unit(x)
        return x
